package site.beanyon.now.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.beanyon.now.mapper.TimePlanMapper;
import site.beanyon.now.model.dto.TimePlanDTO;
import site.beanyon.now.model.po.TimePlanPO;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间计划服务实现类
 */
@Service
public class TimePlanServiceImpl implements TimePlanService {
    @Autowired
    private UserService userService;

    @Autowired
    private TimePlanMapper timePlanMapper;

    /**
     * 创建时间计划
     *
     * @param timePlanPO 时间计划PO
     */
    @Override
    public void createTimePlan(TimePlanPO timePlanPO) {
        this.timePlanMapper.insertTimePlan(timePlanPO);
    }

    /**
     * 删除时间计划
     *
     * @param timePlanId 时间计划id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteTimePlan(Integer timePlanId) {
        return this.timePlanMapper.deleteTimePlan(timePlanId).equals(1);
    }

    /**
     * 获取时间计划列表
     *
     * @return 时间计划DTO列表
     */
    @Override
    public List<TimePlanDTO> listTimePlan() {
        Integer userId = this.userService.getCurrentUser().getId();
        List<TimePlanPO> timePlanPOList = this.timePlanMapper.listTimePlan(userId);
        return this.convertTimePlanPOtoDTO(timePlanPOList);
    }

    /**
     * 获取用户的时间计划列表的活动名称，用于定时创建时间卡片
     *
     * @param userId 用户id
     * @return 活动名称列表
     */
    @Override
    public List<String> listTimePlanActivity(Integer userId) {
        return this.timePlanMapper.listTimePlanActivity(userId);
    }

    /**
     * 更新时间计划的状态
     * @param timePlanId 时间计划id
     * @return 是否更新成功
     */
    @Override
    public boolean updateTimePlanStatus(Integer timePlanId) {
        return this.timePlanMapper.updateTimePlanStatus(timePlanId).equals(1);
    }

    /**
     * 将时间计划PO列表转化为DTO列表
     *
     * @param timePlanPOList 时间计划PO列表
     * @return 时间计划DTO列表
     */
    private List<TimePlanDTO> convertTimePlanPOtoDTO(List<TimePlanPO> timePlanPOList) {
        List<TimePlanDTO> timePlanDTOList = new ArrayList<>();

        for (TimePlanPO timePlanPO : timePlanPOList) {
            timePlanDTOList.add(new TimePlanDTO(timePlanPO));
        }

        return timePlanDTOList;
    }
}
